Declare Sub SetWindowPos Lib "User" (ByVal hWnd As Integer, ByVal hWndInsertAfter As Integer, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal wFlags As Integer)
Const AppTitle = "Steve's Find and Replace"
Const SWP_SHOWWINDOW = &H40
Const HWND_TOPMOST = -1
Sub Action_Click (Index As Integer)
Dim SearchType As Integer
Dim StartPos As Integer
Dim NextPos As Integer
Dim FindVal As String
Dim ReplaceVal As String
Dim SearchLen As Integer
Dim CaseSensitive As Integer
'search constants
Const SRCH_FIND = 0
Const SRCH_VERIFY = 1
Const SRCH_REPLACE = 2
'message box constants
Const MB_QUESTION = 36
Const MB_YES = 6
'if the user selected cancel
If Index = 3 Then
'unload the form
Unload Me
'quit the subroutine
Exit Sub
'otherwise, define the type of Find and Replace
SearchType = Index
End If
'make sure text is entered
If FindText.Text = "" Then
MsgBox "The Search For field must contain one or more characters to look for.", 16, ProgTitle
FindText.SetFocus
Exit Sub
End If
'change pointer to an hourglass
Screen.MousePointer = 11
'set initial values
NextPos = 0 'the next position we start looking for a match
FindVal = FindText.Text 'the text we're looking for
ReplaceVal = ReplaceText.Text 'the text we want to replace with
SearchLen = Len(FindVal) 'the length of the text we're looking for
'the starting position we look for a match
If (FAndRControl.SelText = ReplaceVal) Or (SearchType = SRCH_FIND) Then
'if we've already done this one or we're just finding, we want to go
'past it to the next one
StartPos = FAndRControl.SelStart + 2
'SelStart is 0-based, so we add one to make it one-based (like our text)
StartPos = FAndRControl.SelStart + 1
End If
'if we haven't selected any text yet, we have to start at position 1
If StartPos = 2 Then StartPos = 1
'hide the form so it won't get in the way of the dialog boxes
Me.Visible = False
'read the check box to see if the search is case sensitive